package com.jhy.new2025.BinaryTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 中序遍历
 * 左根右
 */
public class inorderTraversal2 {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;// 左
            }else {
                TreeNode pop = stack.pop();
                int val = pop.val; // 根
                res.add(val);
                cur = cur.right; // 右
            }
        }
        return res;
    }
}
